package com.tuozixuan.util;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import com.tuozixuan.base.Version;

public final class MvnUtils {

	private MvnUtils() {
		
	}
	
	public static String getVersion(String mvnCmd, File projectWorkPath) {
		String versionCommandTemplate = "%s org.apache.maven.plugins:maven-help-plugin:3.1.0:evaluate -Dexpression=project.version -q -DforceStdout";
		String findLocalVersionCommand = String.format(versionCommandTemplate, mvnCmd);
		return CommandUtil.executeCommand(findLocalVersionCommand, projectWorkPath);
	}
	
	public static void changeVersion(String mvnCmd, File projectWorkPath, Version newVersion) {
		String versionSetTemplate = "%s versions:set -DnewVersion=%s -DprocessAllModules=true -DgenerateBackupPoms=false";
		String versionSetCommand = String.format(versionSetTemplate, mvnCmd, newVersion.toString());
		CommandUtil.executeCommand(versionSetCommand, projectWorkPath);
	}
	
	public static void changeDependencyVersion(String mvnCmd, File projectWorkPath, Version newVersion) {
		Map<String, String> replaceMap = new HashMap<>();
		replaceMap.put("<hkcash_server_version>.*?</hkcash_server_version>", "<hkcash_server_version>" + newVersion.toString() + "</hkcash_server_version>");
		
		String rootPom = projectWorkPath.getAbsolutePath() + File.separator + "pom.xml";
		ProjectUtils.replaceFileContent(new File(rootPom), replaceMap);
	}
}
